/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package org.apache.directory.http.client.ui.model;


import java.util.HashMap;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

import org.apache.directory.shared.ldap.constants.SchemaConstants;
import org.apache.directory.shared.ldap.entry.Entry;
import org.apache.directory.shared.ldap.entry.EntryAttribute;


/**
 * TODO EntryTableModel.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class EntryTableModel extends DefaultTableModel
{
    private Entry entry;

    private int rowCount;

    private Map<String, Object> values = new HashMap<String, Object>();

    private static final String[] COLUMNS =
        { "Name", "Value" };

    private int rowPos = 0;


    public void setEntry( Entry entry )
    {
        this.entry = entry;

        rowPos = 0;
        values.clear();
        rowCount = 0;
        
        //first OC
        EntryAttribute oc = entry.get( SchemaConstants.OBJECT_CLASS_AT );

        if ( oc != null )
        {
            rowCount = oc.size();
            populateValueTable( oc );
        }

        for ( EntryAttribute ea : entry )
        {
            if ( ea.getId().equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT ) )
            {
                continue;
            }

            rowCount += ea.size();
            populateValueTable( ea );
        }
    }


    private void populateValueTable( EntryAttribute attr )
    {
        String nameCol = null;
        String valueCol = null;
        String attrId = attr.getId();

        int attrSize = attr.size();

        for ( int i = 0; i < attrSize; i++ )
        {
            nameCol = String.valueOf( rowPos ) + 0;
            values.put( nameCol, attrId );

            valueCol = String.valueOf( rowPos ) + 1;

            Object val = null;
            if ( attr.isHR() )
            {
                val = attr.get( i ).getString();
            }
            else
            {
                val = attr.get( i ).getBytes();
            }

            values.put( valueCol, val );

            rowPos++;
        }
    }


    @Override
    public int getColumnCount()
    {
        return 2;
    }


    @Override
    public String getColumnName( int index )
    {
        return COLUMNS[index];
    }


    @Override
    public int getRowCount()
    {
        return rowCount;
    }


    @Override
    public boolean isCellEditable( int row, int column )
    {
        return false;
    }


    @Override
    public Object getValueAt( int row, int column )
    {
        Object val = values.get( row + "" + column );
        if ( val instanceof byte[] )
        {
            return "Binary Data, length " + ( ( byte[] ) val ).length + " bytes";
        }

        return val;
    }

}
